package com.fengycx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fengycx.config.MinioUtils;
import com.fengycx.exception.GlobalException;
import com.fengycx.pojo.domain.DriverAggrement;
import com.fengycx.mapper.DriverAggrementMapper;
import com.fengycx.pojo.domain.DriverAuthMaterial;
import com.fengycx.service.IDriverAggrementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengycx.service.IDriverAuthMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 司机的协议签署 服务实现类
 * </p>
 *
 * @author Cino
 * @since 2024-02-19
 */
@Service
public class DriverAggrementServiceImpl extends ServiceImpl<DriverAggrementMapper, DriverAggrement> implements IDriverAggrementService {

    @Autowired
    private IDriverAuthMaterialService driverAuthMaterialService;

    @Override
    public void saveContract(DriverAggrement entity) {
        DriverAuthMaterial driverAuth = driverAuthMaterialService.getOne(new LambdaQueryWrapper<DriverAuthMaterial>().eq(DriverAuthMaterial::getId, entity.getDriverId()));
        String driverName = driverAuth.getName();
        DriverAggrement driverAggrement = new DriverAggrement();
        BeanUtil.copyProperties(entity,driverAggrement);
        String sn = RandomUtil.randomStringUpper(3) + RandomUtil.randomNumbers(3).toString() + RandomUtil.randomString(3);
        driverAggrement.setAgreementSn(sn);
        driverAggrement.setCreateTime(new Date());
        driverAggrement.setStatus(2);
        driverAggrement.setDriverName(driverName);
        super.save(driverAggrement);
    }

    @Override
    public String uploadContract(MultipartFile file) {
        MinioUtils util = MinioUtils.getInstance();
        String realFilename = System.currentTimeMillis()+"-"+file.getOriginalFilename();
        if(!file.isEmpty()){
            try {
                util.uploadObject(file.getInputStream(),realFilename,file.getSize());
            } catch (IOException e) {
                e.printStackTrace();
                throw new GlobalException(e.getMessage());
            }
        }
        return MinioUtils.endpoint+"/"+MinioUtils.bucketName+"/"+realFilename;
    }

    @Override
    public void destroyContract(Long contractId) {
        DriverAggrement contract = super.getById(contractId);
        String filePath = contract.getAgreementPathOriginal();
        //这里可以使用常量将minio路径进行%s替换
        List<String> paths = Arrays.asList(filePath);
        MinioUtils util = MinioUtils.getInstance();
        //删除对象
        util.deleteObject(paths);
        contract.setAgreementPathOriginal("");
    }

    @Override
    public String previewContract(Long contractId) {
        DriverAggrement aggrement = super.getById(contractId);
        String url = aggrement.getAgreementPathOriginal();
        return url;
    }
}
